/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import com.pearreview.ui.clock.ScribeClock;

public class HeadingLabel extends JPanel {
	private static final long serialVersionUID = -6916591348341197228L;
	private JLabel caption;
	private ScribeClock scribeClock = null;
	private boolean selected = false;
	private GroupLayout layout;
	private JPanel clock = new JPanel();

	public HeadingLabel(String text) {
		super();
		
		caption = new JLabel(text);
		caption.setUI(new ShadowLabelUI());
		caption.setFont(new Font("Tahoma", Font.BOLD, 12));
		caption.setForeground(new Color(0x333333));
		caption.setHorizontalAlignment(SwingConstants.LEFT);
		caption.setVerticalAlignment(SwingConstants.CENTER);
		caption.setBorder(BorderFactory.createEmptyBorder(10, 148, 10, 0));
		
		clock.setOpaque(false);
		clock.setLayout(new BorderLayout());
		
		JSeparator sep = new JSeparator();
		sep.setUI(new HeadingSeparatorUI());
		
		layout = new GroupLayout(this);
		setLayout(layout);
		
		layout.setHorizontalGroup(layout.createParallelGroup()
				.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
				.addComponent(caption, 
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.PREFERRED_SIZE,
						Short.MAX_VALUE)
				.addComponent(clock,
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.PREFERRED_SIZE)
				.addGap(10)))
				.addComponent(sep,
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.PREFERRED_SIZE,
						Short.MAX_VALUE)
				);
		
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup()
						.addComponent(caption)
						.addComponent(clock,35,35,35))
				.addComponent(sep));

	}
	
	public void setClock(ScribeClock clock) {
		this.scribeClock = clock;
		this.clock.removeAll();
		this.clock.add(clock, BorderLayout.CENTER);
		this.validate();
	}
	
	public ScribeClock getClock() {
		return scribeClock;
	}
	
	public void setCaption(String text, boolean selected) {
		this.caption.setText(text);
		this.selected = selected;
		repaint();
	}
	
	public void setCaption(String text) {
		this.caption.setText(text);
	}
	
	public boolean isSelected() {
		return selected;
	}
	
	public String getCaption() {
		return caption.getText();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		
		Graphics2D g2 = (Graphics2D) g;
		super.paintComponent(g);
		GradientPaint p;
		if (selected) {
			p = new GradientPaint(0, 0, new Color(0xbfd72b),
					0, getHeight()*7, new Color(0xbfd72b));
		} else {
			p = new GradientPaint(0, 0, new Color(0xd4d4d4),
					0, getHeight()*7, new Color(0x333333));
		}
		Paint oldPaint = g2.getPaint();
		g2.setPaint(p);
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setPaint(oldPaint);
	}
}
